package com.yxw.live_vod_backend_boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_backend_boot.dto.CategorySpecificationAddDTO;
import com.yxw.live_vod_backend_boot.dto.CategorySpecificationQueryDTO;
import com.yxw.live_vod_backend_boot.dto.CategorySpecificationUpdateDTO;
import com.yxw.live_vod_backend_boot.entity.YxCategorySpecificationEntity;
import com.yxw.live_vod_backend_boot.entity.YxProductCategoryEntity;
import com.yxw.live_vod_backend_boot.mapper.CategorySpecificationMapper;
import com.yxw.live_vod_backend_boot.mapper.ProductCategoryMapper;
import com.yxw.live_vod_backend_boot.service.CategorySpecificationService;
import com.yxw.live_vod_backend_boot.util.PageUtils;
import com.yxw.live_vod_backend_boot.vo.CategorySpecificationVO;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.PinyinUtill;
import com.yxw.yxnet_cd_center.common.utils.RandomCharacterUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类目规格管理
 * @author xiexuxin
 * @date 2023-12-06
 */
@Service
public class CategorySpecificationServiceImpl extends ServiceImpl<CategorySpecificationMapper, YxCategorySpecificationEntity> implements CategorySpecificationService {

    @Autowired
    private CategorySpecificationMapper specificationMapper;

    @Autowired
    private ProductCategoryMapper categoryMapper;

    /**
     * 添加类目规格
     * @param addDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSpecification(CategorySpecificationAddDTO addDTO) {
        YxProductCategoryEntity category = categoryMapper.selectById(addDTO.getCategoryId());
        // 判断规格名称是否重复
        YxCategorySpecificationEntity existSpecification = baseMapper.selectOne(new QueryWrapper<YxCategorySpecificationEntity>()
                .eq("category_id", addDTO.getCategoryId())
                .eq("specification_name", addDTO.getSpecificationName()));
        if (existSpecification != null) {
            throw new BaseException("类目规格名称已存在");
        }
        YxCategorySpecificationEntity specification = new YxCategorySpecificationEntity();
        specification.setSpecificationName(addDTO.getSpecificationName());
        String s = RandomCharacterUtil.generateBatchNumber();
        String pinYinHeadChar = PinyinUtill.getPinYinHeadChar(addDTO.getSpecificationName());
        specification.setIdentification(pinYinHeadChar + "-" + s);
        specification.setCategoryId(addDTO.getCategoryId());
        specification.setCategoryName(category.getCategoryName());
        specification.setSpecificationValue(String.join(",", addDTO.getSpecificationValue()));
        specification.setCreateTime(new Date());
        baseMapper.insert(specification);
    }

    /**
     * 修改类目规格
     * @param updateDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpecification(CategorySpecificationUpdateDTO updateDTO) {
        YxCategorySpecificationEntity specification = specificationMapper.selectById(updateDTO.getSpecificationId());
        if (specification == null) {
            throw new BaseException("类目规格不存在");
        }
        // 判断规格名称是否重复
        YxCategorySpecificationEntity existSpecification = baseMapper.selectOne(new QueryWrapper<YxCategorySpecificationEntity>()
                .ne("specification_id", updateDTO.getSpecificationId())
                .eq("category_id", specification.getCategoryId())
                .eq("specification_name", updateDTO.getSpecificationName()));
        if (existSpecification != null) {
            throw new BaseException("类目规格名称已存在");
        }
        specification.setSpecificationName(updateDTO.getSpecificationName());
        specification.setSpecificationValue(String.join(",", updateDTO.getSpecificationValue()));
        specification.setUpdateTime(new Date());
        baseMapper.updateById(specification);
    }

    /**
     * 获取类目规格列表
     * @param queryDTO
     * @return
     */
    @Override
    public PageVO<CategorySpecificationVO> getSpecificationList(CategorySpecificationQueryDTO queryDTO) {
        Page<YxCategorySpecificationEntity> page = new Page<>(queryDTO.getCurrPage(), queryDTO.getPageSize());
        String categoryName = StringUtils.trimToNull(queryDTO.getCategoryName());
        String specificationName = StringUtils.trimToNull(queryDTO.getSpecificationName());
        LambdaQueryWrapper<YxCategorySpecificationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(categoryName), YxCategorySpecificationEntity::getCategoryName, categoryName)
                    .like(StringUtils.isNotBlank(specificationName), YxCategorySpecificationEntity::getSpecificationName, specificationName);
        page = baseMapper.selectPage(page, queryWrapper);
        List<YxCategorySpecificationEntity> entityList = page.getRecords();
        List<CategorySpecificationVO> listVoList = new ArrayList<>();
        if (null != entityList && !entityList.isEmpty()) {
            listVoList = BeanUtil.copyToList(entityList, CategorySpecificationVO.class);
        }
        return PageUtils.get(page, listVoList);
    }

    /**
     * 获取类目规格详情
     * @param specificationId
     * @return
     */
    @Override
    public CategorySpecificationVO getSpecificationById(String specificationId) {
        YxCategorySpecificationEntity specification = specificationMapper.selectById(specificationId);
        CategorySpecificationVO vo = new CategorySpecificationVO();
        BeanUtil.copyProperties(specification, vo);
        return vo;
    }

    @Override
    public List<CategorySpecificationVO> getSpecificationListByCategoryId(String categoryId) {
        LambdaQueryWrapper<YxCategorySpecificationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(true, YxCategorySpecificationEntity::getCategoryId, categoryId);
        List<YxCategorySpecificationEntity> entityList = baseMapper.selectList(queryWrapper);
        List<CategorySpecificationVO> listVoList = new ArrayList<>();
        if (null != entityList && !entityList.isEmpty()) {
            listVoList = BeanUtil.copyToList(entityList, CategorySpecificationVO.class);
        }
        return listVoList;
    }


}
